const fs = require('fs');  
const path = require('path');  
const config = require('./config')
// 假设你的文件路径是 './example.js'  
const devFilePath = path.join(__dirname, config.devFilePath);
const buildFilePath = path.join(__dirname, config.buildFilePath);

try { 
    //默认输出的字符串
    let result = '//新建或修改标签,标题随意,内容复制下一行 \njavascript:'
    
    const content = fs.readFileSync(devFilePath, 'utf8');

    //格式成数组，遍历寻找进行替换
    const lines = content.split(/\r?\n/);


    for(let i in lines){
        //取消开始和结束的空格
        let line  = lines[i].trim()


        //根据配置，修改匹配
        let serverURI = 'http://server';
        let replacementString = config.serverScript;
        if(line.includes(serverURI)){
            line = line.replace(new RegExp(serverURI, 'g'), replacementString);  
        }
        

        //替换jquery本地资源
        serverURI = 'http://jquery';
        replacementString = config.jqeryScript;
        if(line.includes(serverURI)){
            line = line.replace(new RegExp(serverURI, 'g'), replacementString);  
        }

        
        //查找是否是备注
        let commentLine = line.substring(0, 2)
        if (
            commentLine !==  '//' &&
            line.length > 0
        ) ( //注释的去掉
            result += line
        )
    }
    fs.writeFileSync(buildFilePath, result, { encoding: 'utf8' });
    console.log('写入完成(100%) ./bulid/setup.js');
    console.log('书签栏内容需要改成:', result);
    
} catch (err) {  
    // 如果读取文件时发生错误，捕获异常并处理  
    console.error('读取文件时发生错误:', err);  
}